// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

namespace SnapObjects.Data
{
    /// <summary>
    ///     Represents an object which only uses raw SQL and SQL parameters to build a SQL statement.
    /// </summary>
    public class SqlRawBuilder : SqlBuilderBase, ISqlRawBuilder
    {
        private RawBuildStatement _statement;

        /// <summary>
        ///     Initializes a new instance of the SqlRawBuilder class.
        /// </summary>
        /// <param name="name"></param>
        public SqlRawBuilder(string name = "") : base(name)
        {
            _statement = new RawBuildStatement();
        }

        internal SqlRawBuilder(string name, bool ignoreSyntaxType) : base(name)
        {
            _statement = new RawBuildStatement(ignoreSyntaxType);
        }

        /// <summary>
        ///     Gets the SQL statement type of the current object.
        /// </summary>
        public override SqlStatementType SqlType => SqlStatementType.SqlRaw;

        internal override SqlBuildStatement Statement => _statement;

        /// <summary>
        ///     Creates a SQL statement by the raw SQL and SQL parameters.
        /// </summary>
        /// <param name="sqlText">A string of raw SQL.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>Returns an ISqlBuilder object which represents the current object.</returns>
        public ISqlBuilder Raw(string sqlText, params ISqlParameter[] parameters)
        {
            _statement.SqlRaw = sqlText;

            _statement.AddParameters(parameters);

            return this;
        }

        public override ISqlBuilder Clone()
        {
            return new SqlRawBuilder(this.Name)
            {
                _statement = (RawBuildStatement)this.Statement.Clone()
            };
        }
    }
}
